package com.wzj.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wzj.yygh.cmn.client.DictFeignClient;
import com.wzj.yygh.enums.DictEnum;
import com.wzj.yygh.hosp.repository.HospitalRepository;
import com.wzj.yygh.hosp.service.HospitalService;
import com.wzj.yygh.model.hosp.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    //将医院基本信息保存到MongoDB
    @Override
    public void save(Map<String, Object> paramMap) {

        //把参数map集合转换对象 Hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在，进行修改
        if(hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {//如果不存在，进行添加
            hospital.setStatus(0); //未上线
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

   @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //医院列表(条件查询分页)
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, String hosname) {
        Sort sort = Sort.by(Sort.Direction.ASC, "hoscode");
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //hospitalSetQueryVo转换Hospital对象
        Hospital hospital = new Hospital();
        hospital.setHosname(hosname);
        //创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        //调用方法实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //获取查询list集合，遍历进行医院等级封装
        pages.getContent().forEach(this::packHospital);

        return pages;
    }
    //获取查询list集合，遍历进行医院等级封装
    private void packHospital(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(null,hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(null,hospital.getCityCode());
        String districtString = dictFeignClient.getName(null,hospital.getDistrictCode());
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString+hospital.getAddress());
        hospital.getParam().put("hostypeString",hostypeString);
    }

    //更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHospById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        //医院基本信息（包含医院等级）
//        result.put("hospital",hospital);
//        //单独处理更直观
        hospital.getParam().put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
//        hospital.setBookingRule(null);
        this.packHospital(hospital);
        return hospital;
    }

    //获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }
    //根据医院名称、级别和区域查询医院列表
    @Override
    public List<Hospital> selectList(String keywords, String levelId, String areaId) {
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withMatcher("hosname",ExampleMatcher.GenericPropertyMatchers.contains())//模糊查询
                .withMatcher("hostype",ExampleMatcher.GenericPropertyMatchers.exact()) //精确查询
                .withMatcher("districtCode",ExampleMatcher.GenericPropertyMatchers.exact());
        //创建查询对象
        Hospital hospital = new Hospital();
        hospital.setHosname(keywords);
        hospital.setHostype(levelId);
        hospital.setDistrictCode(areaId);

        Example<Hospital> example = Example.of(hospital, matcher);
        //执行查询
        List<Hospital> list = hospitalRepository.findAll(example);

        //封装医院等级数据
        list.forEach(this::packHospital);

        return list;
    }

    @Override
    public Hospital getDetail(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        this.packHospital(hospital);
        return hospital;
    }

    //根据医院名称查询
//    @Override
//    public List<Hospital> findByHosname(String hosname) {
//        return hospitalRepository.findHospitalByHosnameLike(hosname);
//    }
//
//    //根据医院编号获取医院预约挂号详情
//    @Override
//    public Map<String, Object> item(String hoscode) {
//        Map<String, Object> result = new HashMap<>();
//        //医院详情
//        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
//        result.put("hospital", hospital);
//        //预约规则
//        result.put("bookingRule", hospital.getBookingRule());
//        //不需要重复返回
//        hospital.setBookingRule(null);
//        return result;
//    }

}
